﻿// Jinx AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Core
{
    public static class Series
    {
        public static Series<T> New<T>(IEnumerable<T> E)
        {
            return new Series<T>(E);
        }
    }

    public class Series<T> : IEnumerable<T>
    {
        private readonly IEnumerable<T> Enumerable;
        private T[] _a;

        public Series(IEnumerable<T> Enumerable)
        {
            this.Enumerable = Enumerable;
        }

        public Series(params T[] Params) : this((IEnumerable<T>) Params)
        {
        }

        public T this[int index]
        {
            get { return Array[index]; }
        }

        public int Length
        {
            get { return Array.Length; }
        }

        private T[] Array
        {
            get
            {
                Eval();
                return _a;
            }
        }

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            foreach (var t in Array) yield return t;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        public Series<T> Eval()
        {
            if (_a == null) _a = Enumerable.ToArray();
            return this;
        }

        public override bool Equals(object that)
        {
            if (that == null) throw new ArgumentNullException();

            if (GetType() != that.GetType())
                return false;

            if (ReferenceEquals(this, that))
                return true;

            return ImmutableUtil.ArrayEquals(Array, (that as Series<T>).Array);
        }

        public override int GetHashCode()
        {
            return ImmutableUtil.ArrayHash(Array);
        }

        public override string ToString()
        {
            return "[" + Array.Join(", ") + "]";
        }

        public static bool operator ==(Series<T> s1, Series<T> s2)
        {
            var NullCheck = ImmutableUtil.NullCheck(s1, s2);
            if (NullCheck.HasValue) return NullCheck.Value;

            return s1.Equals(s2);
        }

        public static bool operator !=(Series<T> s1, Series<T> s2)
        {
            return !(s1 == s2);
        }
    }
}